package com.spring.service.impl;

import com.spring.dao.ModuleDao;
import com.spring.dao.UserDao;
import com.spring.pojo.SysUser;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;




@Service("userService")
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private ModuleDao moduleDao;
    @Override
    public SysUser login(String userCode, String psw) {
        Map<String,Object> params = new HashMap<String,Object>() ;
        params.put("userCode",userCode);
        params.put("psw",psw);
        return userDao.selectByNameAndPwd(params);
    }

    @Override
    public SysUser register(String userCode, String psw, String name) {
        if(isExist(userCode)){
            return null;
        }
        Map<String,Object> params =new HashMap<String,Object>();
        params.put("userCode",userCode);
        params.put("psw",psw);
        params.put("name",name);
        return userDao.insertNewUser(params);
    }
    @Override
    public boolean isExist(String userCode){
        SysUser user=userDao.isExistByCode(userCode);
        return user!=null;
    }
    @Override
    public List<SysUser> selectUsers(String name){
        return userDao.selectUsersByName(name);
    }

}
